<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../vue.js"></script>
</head>

<body>
    <div id="app">
        <input type="text" v-model="str"><br/>
        <button @click = "sort(0)">正常排序</button>
        <button @click = "sort(1)">升序</button>
        <button @click = "sort(2)">降序</button>

        <ul>
            <li v-for = "item in person2" :key = "item.id">
                姓名:{{item.name}}--年龄:{{item.age}}--性别:{{item.gender}}
            </li>
        </ul>

    </div>
    <script>
        Vue.config.productionTip = false;

        const vm = new Vue({
            el: "#app",
            data() {
                return {
                    str: "",
                    person: [
                        { id: '001', name: '马冬梅', age: 25, gender: '女' },
                        { id: '002', name: '周冬雨', age: 20, gender: '女' },
                        { id: '003', name: '周杰伦', age: 21, gender: '男' },
                        { id: '004', name: '帅邓伦', age: 22, gender: '男' },
                    ],
                    type: 0
                }
            },
            //计算属性根据str得到不同的数组
            computed:{
                person2(){
                    //过滤
                    const filterArr = this.person.filter((item) =>{
                        return item.name.indexOf(this.str) !== -1
                    })

                    //默认正常排序
                    if(this.type === 0){
                        return filterArr
                    }

                    //
                    const sortArr = filterArr.sort((a , b)=>{
                        return this.type === 1 ? a.age-b.age : b.age-a.age
                    })


                    return sortArr
                }
            },
            methods:{
                sort(num){
                    this.type = num
                }
            }
        })
    </script>
</body>

</html>